package com.example.contact_recover;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.example.contact_recover.models.ServerRes;
import com.example.contact_recover.models.User;
import com.example.contact_recover.util.NetUtil;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class LoginActivity extends AppCompatActivity {

    private Button btn_login;
    private EditText edit_phone;
    private EditText edit_passwd;
    private String phone;
    private String passwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setTitle(R.string.login);


        TextView textSign = findViewById(R.id.text_sign);

        // 用户注册..
        textSign.setOnClickListener((view)->{

            startActivity(new Intent(this,SignActivity.class));
        });


        edit_phone = findViewById(R.id.edit_phone);
        edit_passwd = findViewById(R.id.edit_passwd);


        // 用户登录
        btn_login = findViewById(R.id.btn_login);
        btn_login.setOnClickListener((view) ->{
            System.out.println("call back ");
            loginCallBack loginCallBack = new loginCallBack();

            phone = edit_phone.getText().toString().trim();
            passwd = edit_passwd.getText().toString().trim();
            NetUtil.Commit2Server(
                    NetUtil.SERVER_URL,  // url
                    phone,               // phone
                    passwd,              // passwd
                    loginCallBack        // call back
            );
        });

        TextView tv_back_passwd = findViewById(R.id.text_back_passwd);

        tv_back_passwd.setOnClickListener((view)->{
             //  goto back passwd

            startActivity(new Intent(this, BackWdActivity.class));
        });
    }


    // 用户登录回调实现
    class loginCallBack implements Callback {

        @Override
        public void onFailure(Call call, IOException e) {

            System.out.println("failure "+ e.toString());
        }

        // login success save last login
        // goto the main activity
        @Override
        public void onResponse(Call call, Response response) throws IOException {


            byte[] bytes = response.body().bytes();

            if (bytes == null){
                return ;
            }

            String text = new String(bytes, 0, bytes.length);

            ServerRes res = JSON.parseObject(text, ServerRes.class);

            if (res.getCode() != ServerRes.Ok){
                Snackbar.make(btn_login,R.string.request_error + res.getMessage(),Snackbar.LENGTH_SHORT).show();
                return ;
            }

            SharedPreferences lastLogin = getSharedPreferences("last_login", MODE_PRIVATE);

            SharedPreferences.Editor editor = lastLogin.edit();

            // todo 这里需要做加密处理
            editor.putString(getResources().getString(R.string.title_phone),phone);
            editor.putString(getResources().getString(R.string.title_passwd),passwd);
            User user = new User();
            System.out.println("u id "+ Long.parseLong(res.getMessage()));
            user.setId(Long.parseLong(res.getMessage()));
            user.setPasswd(passwd);
            user.setPhone(phone);
            user.save();
            editor.putLong("id",user.getId());
            // 保存 登录信息
            editor.apply();

            finish();

            startActivity(new Intent(LoginActivity.this,MainActivity.class));
        }
    }
}
